/**
 *****************************************************************************
 * @file    hal_spi_flash.h
 * @brief    -
 *
 * Copyright (C) RealMega 2019-2020
 *
 *****************************************************************************
*/

#ifndef __HAL_SPI_FLASH_H__
#define __HAL_SPI_FLASH_H__

#define HAL_SPI_FLASH_ADDR_MASK         0xFFFFFF

enum SPI_FLASH_CMD_ENUMERATION
{
    FLASH_CMD_WRITE_STATUS_REG          = 0x01,
    FLASH_CMD_PAGE_PROGRAM              = 0x02,
    FLASH_CMD_READ_STATUS_REG1          = 0x05,
    FLASH_CMD_WRITE_ENABLE              = 0x06,
    FLASH_CMD_SECTOR_ERASE              = 0x20,
    FLASH_CMD_READ_UNIQUE_ID            = 0x4B,
    FLASH_CMD_BLOCK32_ERASE             = 0x52,
    FLASH_CMD_CHIP_ERASE                = 0x60,
    FLASH_CMD_REMS                      = 0x90,
    FLASH_CMD_BLOCK64_ERASE             = 0xD8,
    FLASH_CMD_RELEASE_POWER_DOWN        = 0xAB,
    FLASH_CMD_DISABLE_QPI               = 0xFF,
};

enum SPI_FLASH_READ_MODE
{
    SPI_FLASH_READ_SERIAL_MODE          = 0x0,
    SPI_FLASH_READ_DUAL_MODE            = 0x1,
    SPI_FLASH_READ_QUARD_MODE           = 0x2,
    SPI_FLASH_READ_MODE_RESERVED,
};

enum SPI_FLASH_CMD_MODE
{
    SPI_FLASH_CMD_SERIAL_MODE           = 0x0,
    SPI_FLASH_CMD_DUAL_MODE             = 0x1,
    SPI_FLASH_CMD_QUARD_MODE            = 0x2,
    SPI_FLASH_CMD_MODE_RESERVED,
};

void hal_spi_flash_init(uint8_t read_mode, uint8_t cmd_mode, uint8_t clk_div);

void hal_spi_flash_disable_read(void);
void hal_spi_flash_enable_read(void);

void hal_spi_flash_erase(uint8_t cmd, uint32_t addr);
void hal_spi_flash_send_command(uint8_t cmd);
void hal_spi_flash_page_program(uint8_t cmd, uint32_t addr);
bool hal_spi_flash_send_data(const uint8_t *data, uint32_t length, bool quard_flag);
uint8_t hal_spi_flash_read_status_register(uint8_t cmd);

bool hal_spi_flash_flush_fifo(bool clr_rx, bool clr_tx);
void hal_spi_flash_enable_continuous_mode(bool enable);

uint32_t hal_spi_flash_recv_data(uint8_t* buf, uint32_t length);
bool hal_spiflash_is_txfifo_empty(void);

#endif

